package com.wangsd.web.mqpay.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.mqpay.entity.Customer;
import com.wangsd.web.mqpay.entity.Passageway;
import com.wangsd.web.mqpay.service.CustomerPassagewayService;
import com.wangsd.web.mqpay.service.PassagewayService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "客户(商圈)管理")
@RestController
@RequestMapping("/mqpay/passageway")
@Slf4j
public class PassagewayController extends MyController {
    @Autowired
    private PassagewayService service;
    @Autowired
    private CustomerPassagewayService customerPassagewayService;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             Passageway queryObj) {
        log.debug("分页查询列表-controller");
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Passageway> list = service.selectPage(queryObj, loginUser.getCode());
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody Passageway entity) {
        log.debug("新增或者修改-controller");
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "删除通道")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        log.debug("删除通道-controller");
        service.delete(id);
        return Result.success();
    }

    @ApiOperation(value = "查询所有通道（下拉列表）")
    @PostMapping("/selectAllPassageway")
    public Result selectAllPassageway() {
        log.debug("查询所有通道（下拉列表）-controller");
        SysUser loginUser = getUserInfo();
        String code = loginUser.getCode();
        if (loginUser.getType() == 3) {
            code = code.substring(0, code.lastIndexOf("-"));
        }
        List<Passageway> list = service.selectAllPassageway(code);
        return Result.success(list);
    }

    @ApiOperation(value = "根据客户id查询配置的通道")
    @GetMapping("/{id}/selectWays")
    public Result selectWays(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        log.debug("根据客户id查询配置的通道-controller");
        List<Passageway> list = service.selectWays(id);
        return Result.success(list);
    }

    @ApiOperation(value = "配置客户通道")
    @PostMapping("/addPassageway")
    public Result addPassageway(@RequestBody Customer customer) {
        log.debug("配置客户通道-controller");
        customerPassagewayService.addPassageway(customer);
        return Result.success();
    }
}
